﻿/*
    Parker for Kaixin001.com 

    Copyright (C) 2008, TurboZV, turbozv@gmail.com, http://www.turbozv.com/

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.IO;
using System.Collections.ObjectModel;
using Helper;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace Parker
{
    public class UserConfigData
    {
        public string Email;
        public string Password;

        public int MasterId = -1; // 主人家

        public bool DoParking = true; // 是否停车
        public bool DoPost = false; // 是否贴牌
        public bool DoPostLess15 = false;  //小于15分钟的是否贴条
        public bool DoCall = false; // 是否报警

        public bool DoAutoUpgradeGarage = false; //自动买卡,买卡自动升级免费车位        
        public int DoAutoMaxCarNumber = 0; //最大车数. 当拥有的车数不到次数时会自动买车 10最大,0显然不买
        public int DoAutoUpgradeCar = 0; //自动升级车,0=不升级，1=先升级便宜的，2=先升贵的
        public int CarColor = 16711680; //默认自动升级车的颜色为红色

        public Collection<int> WhiteList = new Collection<int>(); // 停车白名单
        public Collection<int> BlackList = new Collection<int>(); // 停车黑名单
        public Collection<int> NoPostList = new Collection<int>(); // 不贴条白名单
        public Collection<int> NoCallList = new Collection<int>(); // 不贴条白名单
        public Collection<int> NoParkingList = new Collection<int>(); // 不停车名单
        public Collection<int> NoBuyCarIdList = new Collection<int>(); //不买车黑名单
        public Collection<int> NoUpgradeCarIdList = new Collection<int>(); //不升级车黑名单
        public Dictionary<int, int> PostList = new Dictionary<int, int>(); // 贴条的详细设置
    }

    public class ConfigHelper
    {
        public static UserConfigData ReadConfigData(string filename)
        {
            UserConfigData config = new UserConfigData();
            try
            {
                string[] lines = File.ReadAllLines(filename);
                foreach (string line in lines)
                {
                    if (line.StartsWith("Email=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.Email = GlobalConfigHelper.GetStringValue(line);
                        continue;
                    }

                    if (line.StartsWith("Password=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.Password = GlobalConfigHelper.GetStringValue(line);
                        continue;
                    }

                    if (line.StartsWith("MasterId=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.MasterId = GlobalConfigHelper.GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("DoPost=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.DoPost = GlobalConfigHelper.GetBoolValue(line);
                        continue;
                    }

                    if (line.StartsWith("DoCall=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.DoCall = GlobalConfigHelper.GetBoolValue(line);
                        continue;
                    }

                    if (line.StartsWith("DoParking=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.DoParking = GlobalConfigHelper.GetBoolValue(line);
                        continue;
                    }

                    if (line.StartsWith("DoPostLess15=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.DoPostLess15 = GlobalConfigHelper.GetBoolValue(line);
                        continue;
                    }

                    if (line.StartsWith("WhiteList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GlobalConfigHelper.GetIntegerListValue(config.WhiteList, line);
                        continue;
                    }

                    if (line.StartsWith("BlackList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GlobalConfigHelper.GetIntegerListValue(config.BlackList, line);
                        continue;
                    }

                    if (line.StartsWith("NoPostList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GlobalConfigHelper.GetIntegerListValue(config.NoPostList, line);
                        continue;
                    }

                    if (line.StartsWith("NoCallList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GlobalConfigHelper.GetIntegerListValue(config.NoCallList, line);
                        continue;
                    }

                    if (line.StartsWith("NoParkingList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GlobalConfigHelper.GetIntegerListValue(config.NoParkingList, line);
                        continue;
                    }

                    if (line.StartsWith("DoAutoUpgradeGarage=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.DoAutoUpgradeGarage = GlobalConfigHelper.GetBoolValue(line);
                        continue;
                    }

                    if (line.StartsWith("NoBuyCarIdList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GlobalConfigHelper.GetIntegerListValue(config.NoBuyCarIdList, line);
                        continue;
                    }

                    if (line.StartsWith("NoUpgradeCarIdList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GlobalConfigHelper.GetIntegerListValue(config.NoUpgradeCarIdList, line);
                        continue;
                    }

                    if (line.StartsWith("DoAutoMaxCarNumber=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.DoAutoMaxCarNumber = GlobalConfigHelper.GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("CarColor=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.CarColor = GlobalConfigHelper.GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("DoAutoUpgradeCar=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.DoAutoUpgradeCar = GlobalConfigHelper.GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("Post=", StringComparison.OrdinalIgnoreCase))
                    {
                        Match match = Regex.Match(GlobalConfigHelper.GetStringValue(line), @" *([0-9]+|\*) *, *([0-9]+|\*) *");
                        if (match.Success)
                        {
                            string struid = match.Result("$1");
                            string strprofit = match.Result("$2");

                            int uid;
                            int profit;

                            if (!Int32.TryParse(struid, out uid))
                            {
                                uid = -1;
                            }
                            if (!Int32.TryParse(strprofit, out profit))
                            {
                                profit = -1;
                            }
                            config.PostList[uid] = profit;
                        }
                    }

                }
            }
            catch (Exception e)
            {
                TraceLog.Error(e.Message);
                return null;
            }

            if (config.Email == null || config.Password == null)
            {
                TraceLog.Error("文件格式不正确！");
                return null;
            }

            if (config.MasterId > 0)
            {
                config.NoPostList.Remove(config.MasterId);
                config.NoPostList.Add(config.MasterId);
            }
            return config;
        }
    }
}